#include "TuYa_BLE.h"

//unsigned char BLE_RX_FrameType;
//unsigned char BLE_RX_FrameDataLength;
//unsigned char BLE_RX_FrameCherkSum;
//unsigned char BLE_RX_Is_FrameHead;
//unsigned char BLE_RX_Is_Frame;
//
//unsigned char gbv_BLE_IS_RX_Connect_Status;
//unsigned char gbv_BLE_IS_TX_Connect_Status;
//
//unsigned char gbv_BLE_IS_RX_Request_AllData;
//unsigned char gbv_BLE_IS_TX_Request_AllData;
//
//unsigned char gbv_BLE_IS_RX_UploadData;
//
//
//unsigned char gbv_BLE_IS_TX_Request_Time;
//unsigned char gbv_BLE_IS_TX_Heart_Ack;
//unsigned char gbv_BLE_IS_TX_Information;
//unsigned char gbv_BLE_IS_RX_Information;
//unsigned char gbv_BLE_IS_TX_Vision;
//unsigned char gbv_BLE_IS_TX_BLEWorkMode;

volatile BLE_t BLE;

const unsigned char TuYa_PID[] = "lghzz0u2";


void fun_MCU_Respone_ConnectStatus()
{
    DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_CONNECT_STATUS;
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x00;
    unsigned char i,cherksum = 0;
    for(i=0;i<6;i++)
    {
        cherksum += DrvUartFormat.TxBuf[i];
    }
    DrvUartFormat.TxBuf[6] = cherksum;
    fun_UART_Tx_Data(7);
}
void fun_MCU_UpLoad_Data(unsigned char dpid,unsigned char datatype,long data,unsigned int datalength)
{
    unsigned char i,cherksum = 0;
    unsigned int length = datalength + 4;
    DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_UPLOAD_DATA;
    DrvUartFormat.TxBuf[4] = length >>8;
    DrvUartFormat.TxBuf[5] = length;
    DrvUartFormat.TxBuf[6] = dpid;
    DrvUartFormat.TxBuf[7] = datatype;
    DrvUartFormat.TxBuf[8] = datalength >>8;
    DrvUartFormat.TxBuf[9] = datalength;
    for(i=10;i<datalength+10;i++)
    {
        DrvUartFormat.TxBuf[i] = data>>(8*(9+datalength-i));
    }
    for(i=0;i<datalength+10;i++)
    {
        cherksum += DrvUartFormat.TxBuf[i];
    }
    DrvUartFormat.TxBuf[datalength+10] = cherksum;
    fun_UART_Tx_Data(datalength+11);
}
void fun_MCU_Request_Time()
{
    DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_REALTIME;
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x01;
    DrvUartFormat.TxBuf[6] = 0x02;
    DrvUartFormat.TxBuf[7] = 0xE3;
    fun_UART_Tx_Data(8);
}
void fun_MCU_Ack_Heart()
{
    DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_HEART_ACK;
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x01;
    DrvUartFormat.TxBuf[6] = 0x00;
    DrvUartFormat.TxBuf[7] = 0x00;
    fun_UART_Tx_Data(8);
}
void fun_MCU_Ack_Information()
{
    DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_INFORMATION;
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x0D;
    DrvUartFormat.TxBuf[6] = TuYa_PID[0];
    DrvUartFormat.TxBuf[7] = TuYa_PID[1];
    DrvUartFormat.TxBuf[8] = TuYa_PID[2];
    DrvUartFormat.TxBuf[9] = TuYa_PID[3];
    DrvUartFormat.TxBuf[10] = TuYa_PID[4];
    DrvUartFormat.TxBuf[11] = TuYa_PID[5];
    DrvUartFormat.TxBuf[12] = TuYa_PID[6];
    DrvUartFormat.TxBuf[13] = TuYa_PID[7];
    DrvUartFormat.TxBuf[14] = 0x31;//保留字節，固定值
    DrvUartFormat.TxBuf[15] = 0x2E;//保留字節，固定值
    DrvUartFormat.TxBuf[16] = 0x30;//保留字節，固定值
    DrvUartFormat.TxBuf[17] = 0x2E;//保留字節，固定值
    DrvUartFormat.TxBuf[18] = 0x30;//保留字節，固定值
    unsigned char i,cherksum = 0;
    for(i=0;i<19;i++)
    {
    cherksum += DrvUartFormat.TxBuf[i];
    }
    DrvUartFormat.TxBuf[19] = cherksum;
    fun_UART_Tx_Data(20);
}
void fun_MCU_Ack_Vision()
{
    DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_MCU_VISION;
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x06;
    DrvUartFormat.TxBuf[6] = 0x01;
    DrvUartFormat.TxBuf[7] = 0x00;
    DrvUartFormat.TxBuf[8] = 0x00;
    DrvUartFormat.TxBuf[9] = 0x01;
    DrvUartFormat.TxBuf[10] = 0x00;
    DrvUartFormat.TxBuf[11] = 0x00;
    unsigned char i,cherksum = 0;
    for(i=0;i<12;i++)
    {
    cherksum += DrvUartFormat.TxBuf[i];
    }
    DrvUartFormat.TxBuf[12] = cherksum;
    fun_UART_Tx_Data(13);
}
void fun_MCU_Ack_BLEWorkMode()
{
    DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_WORKMODE;
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x00;
    DrvUartFormat.TxBuf[6] = 0x01;
    fun_UART_Tx_Data(7);
}
void fun_MCU_Tx_LowPowerOn()
{
	DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_LOWPOWERON;//0xE5
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x01;
    DrvUartFormat.TxBuf[6] = 0x01;//打開低功耗功能
    DrvUartFormat.TxBuf[7] = 0xE6;
    fun_UART_Tx_Data(8);	
}
void fun_MCU_Tx_SysClockOff()
{
	DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_SYSCLOCKOFF;//0xE4
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x01;
    DrvUartFormat.TxBuf[6] = 0x00;//关闭系统计时器
    DrvUartFormat.TxBuf[7] = 0xE4;
    fun_UART_Tx_Data(8);	
}
void fun_MCU_Tx_Disconnect()
{
	DrvUartFormat.TxBuf[0] = 0x55;
    DrvUartFormat.TxBuf[1] = 0xAA;
    DrvUartFormat.TxBuf[2] = 0x00;
    DrvUartFormat.TxBuf[3] = BLE_DISCONNECT;//0xE7
    DrvUartFormat.TxBuf[4] = 0x00;
    DrvUartFormat.TxBuf[5] = 0x00;
    DrvUartFormat.TxBuf[6] = 0xE6;
    fun_UART_Tx_Data(7);	
}

void fun_BLE_Request_AllData()
{
	BLE.flag.b.Is_TX_Lock 		= 1;
	BLE.flag.b.Is_TX_Start 		= 1;
	BLE.flag.b.Is_TX_UnLock 	= 1;
	BLE.flag.b.Is_TX_BatValue 	= 1;
	BLE.flag.b.Is_TX_BleState 	= 1;
}
void fun_BLE_Rx_Protocol()
{
	if(DrvUartFormat.RxTimeOutCnt > 100 / TIMER_MS)//100ms接收超时
    {
        DrvUartFormat.RxTimeOutCnt = 100 / TIMER_MS + 1;
        DrvUartFormat.RxBufoffset = 0;
        BLE.flag.b.Is_RX_FrameHead = 0;
    }
    if(BLE.flag.b.Is_RX_Frame == 1)//成功接收
    {
        DrvUartFormat.TxCycle = 0;
        BLE.flag.b.Is_RX_Frame = 0;
        //数据解析
        switch(BLE.FrameType)
        {
            case BLE_INFORMATION:
                BLE.flag.b.Is_RX_Information = 1;
                BLE.flag.b.Is_TX_Information = 1;
            break;
            case BLE_MCU_VISION:
                BLE.flag.b.Is_TX_Vision = 1;
                BLE.flag.b.Is_Connect = 1;
            break;
            case BLE_WORKMODE:
                BLE.flag.b.Is_TX_BLEWorkMode = 1;
            break;
            case BLE_HEART_ACK:
               BLE.flag.b.Is_TX_Heart_Ack = 1;
            break;
            case BLE_CONNECT_STATUS://BLE连接状态
                BLE.flag.b.Is_Tx_Connect_status = 1;
                BLE.ConnecStatus = DrvUartFormat.RxBuf[6];
                if(BLE.ConnecStatus == TUYA_IS_NOBOUND)
                {
                	BLE.flag.b.Is_Connect = 0;	
                }
                else if(BLE.ConnecStatus == TUYA_IS_BOUND_NOCONNECT)
                {
                	BLE.flag.b.Is_Connect = 0;	
                }
                else if(BLE.ConnecStatus == TUYA_IS_BOUND_CONNECT)
                {
                	BLE.flag.b.Is_Connect = 1;	
                }
            break;
            case BLE_LOWPOWERON:
            	if(DrvUartFormat.RxBuf[6] == 0x00) //成功
            	{
            		BLE.flag.b.Tx_LowPower_On = 0;
            		BLE.flag.b.Rx_LowPower_On = 1;
            	}
            	else
            	{
            			
            	}
            break;
            case BLE_SYSCLOCKOFF:
            	if(DrvUartFormat.RxBuf[6] == 0x00) //成功
            	{
            		BLE.flag.b.Tx_SysClock_Off = 0;
            		BLE.flag.b.Rx_SysClock_Off = 1;
            	}
            	else
            	{
            			
            	}
            break;
            case BLE_DISCONNECT:
            	if(DrvUartFormat.RxBuf[6] == 0x00) //成功
            	{
            		BLE.flag.b.Tx_Disconnect = 0;
            		BLE.flag.b.Rx_Disconnect = 1;
            	}
            	else
            	{
            			
            	}
            break;

            case BLE_UPLOAD_DATA:
                //不需要回复
            break;
            case BLE_REQUEST_ALLDATA:
                //BLE.flag.b.Is_RX_Request_AllData = 1;//不需要判断，直接回复数据
                //BLE.flag.b.Is_TX_Request_AllData = 1;//不需要回复
                fun_BLE_Request_AllData();
                
            break;
            case BLE_ISSUED_DATA://接收下发的数据，MCU回复最新的     
            	BLE.flag.b.Is_TX_BatValue = 1;         
                if(DrvUartFormat.RxBuf[6] == TUYA_DPID_LOCK)
                {
                    BLE.flag.b.Is_RX_Lock = 1;
                    BLE.flag.b.Is_TX_Lock = 1;
                }
                else if(DrvUartFormat.RxBuf[6] == TUYA_DPIP_START)
                {
                    BLE.flag.b.Is_RX_Start = 1;
                    BLE.flag.b.Is_TX_Start = 1;
                }
                else if(DrvUartFormat.RxBuf[6] == TUYA_DPID_UNLOCK)
                {
                    BLE.flag.b.Is_RX_UnLock = 1; 
                    BLE.flag.b.Is_TX_UnLock = 1;                  
                }
            break;
            case BLE_REALTIME:
//                BLE.flag.b.Is_TX_Request_Time = 0;
//                TuYa_Year   = BLE_RXBuf[8];
//                TuYa_Month  = BLE_RXBuf[9];
//                TuYa_Day    = BLE_RXBuf[10];
//                TuYa_Hour   = BLE_RXBuf[11];
//                TuYa_Min    = BLE_RXBuf[12];
//                TuYa_Sec    = BLE_RXBuf[13];
//                TuYa_Week   = BLE_RXBuf[14];

            break;
           
            default:
            break;
        }
    }
}

void fun_BLE_TX_Protocol()
{
    if(DrvUartFormat.TxCycle >= 400 / TIMER_MS)
    {
        DrvUartFormat.TxCycle = 0;
        if(BLE.flag.b.Is_TX_Information)
        {
            BLE.flag.b.Is_TX_Information = 0;
            fun_MCU_Ack_Information();
        }
        else if(BLE.flag.b.Is_TX_Vision)
        {
            BLE.flag.b.Is_TX_Vision = 0;
            fun_MCU_Ack_Vision();
        }
        else if(BLE.flag.b.Is_TX_BLEWorkMode)
        {
            BLE.flag.b.Is_TX_BLEWorkMode = 0;
            fun_MCU_Ack_BLEWorkMode();
        }
        else if(BLE.flag.b.Is_TX_Heart_Ack)
        {
            BLE.flag.b.Is_TX_Heart_Ack = 0;
            fun_MCU_Ack_Heart();
        }
        else if(BLE.flag.b.Is_Tx_Connect_status)
        {
            BLE.flag.b.Is_Tx_Connect_status = 0;
            fun_MCU_Respone_ConnectStatus();
        }
        else if(BLE.flag.b.Is_TX_Request_Time)
        {
            BLE.flag.b.Is_TX_Request_Time = 0;
            fun_MCU_Request_Time();
        }
        else if(BLE.flag.b.Tx_LowPower_On)
        {
        	fun_MCU_Tx_LowPowerOn();	
        }
        else if(BLE.flag.b.Tx_SysClock_Off)
        {
        	fun_MCU_Tx_SysClockOff();
        }
        
        	
        else if(BLE.flag.b.Is_TX_Lock)
        {
            BLE.flag.b.Is_TX_Lock  = 0;
            fun_MCU_UpLoad_Data(TUYA_DPID_LOCK,TUYA_DATA_TYPE_BOLL,LOCK_ON,1);
        }
        else if(BLE.flag.b.Is_TX_Start)
        {
            BLE.flag.b.Is_TX_Start = 0;
            fun_MCU_UpLoad_Data(TUYA_DPIP_START,TUYA_DATA_TYPE_BOLL,START_ON,1);
        }
        else if(BLE.flag.b.Is_TX_UnLock)
        {
            BLE.flag.b.Is_TX_UnLock = 0;
            fun_MCU_UpLoad_Data(TUYA_DPID_UNLOCK,TUYA_DATA_TYPE_BOLL,UNLOCK_ON,1);
        }
        else if(BLE.flag.b.Is_TX_BleState)
        {
            BLE.flag.b.Is_TX_BleState = 0;
            fun_MCU_UpLoad_Data(TUYA_DPID_BLESTATE,TUYA_DATA_TYPE_ENUM,BLE.flag.b.Is_Connect,1);
        }
        else if(BLE.flag.b.Is_TX_BatValue)
        {
            BLE.flag.b.Is_TX_BatValue = 0;
            unsigned char batvalue;
            batvalue = fun_Get_BATValue();
            fun_MCU_UpLoad_Data(TUYA_DPID_BATVALUE,TUYA_DATA_TYPE_VALUE,batvalue,4);
        }
        
        else if(BLE.flag.b.Tx_Disconnect)
        {
        	fun_MCU_Tx_Disconnect();	
        }
        
    }
}
void fun_BLE_Rx_Data(unsigned char data)
{
    DrvUartFormat.RxTimeOutCnt = 0;
    DrvUartFormat.RxBuf[DrvUartFormat.RxBufoffset] = data;
    DrvUartFormat.RxBufoffset ++;
    if(DrvUartFormat.RxBuf[0] != 0x55)
    {
    	DrvUartFormat.RxBufoffset = 0;	
    }
    if(DrvUartFormat.RxBufoffset == 6)
    {
        if(DrvUartFormat.RxBuf[0] == 0x55 && DrvUartFormat.RxBuf[1] == 0xAA)
        {
            BLE.FrameType = DrvUartFormat.RxBuf[3];
            BLE.RXLength = DrvUartFormat.RxBuf[4] + DrvUartFormat.RxBuf[5];//char型够了
            BLE.flag.b.Is_RX_FrameHead = 1;
        }
        else
        {
            BLE.RXLength = 0xff;
            DrvUartFormat.RxBufoffset = 0;
        }
    }
    if(BLE.flag.b.Is_RX_FrameHead == 1)
    {
        if(DrvUartFormat.RxBufoffset == BLE.RXLength + 7)
        {
            unsigned char i,cherksum = 0;
            BLE.flag.b.Is_RX_FrameHead = 0;
            for(i=0;i<DrvUartFormat.RxBufoffset-1;i++)
            {
                cherksum += DrvUartFormat.RxBuf[i];
            }
            if(cherksum == DrvUartFormat.RxBuf[DrvUartFormat.RxBufoffset-1])//cherksum
            {
                BLE.flag.b.Is_RX_Frame = 1;//校验和正确，接收成功
            }
        }
    }
}

void fun_BLE_TO_HHYLOCK_315MHz()
{
	if(BLE.flag.b.Is_RX_Lock)
	{
		BLE.flag.b.Is_RX_Lock = 0;
		HHYLock.KeyValue = KEY_LOCK;	
	}
	else if(BLE.flag.b.Is_RX_Start)
	{
		
		BLE.flag.b.Is_RX_Start = 0;
		HHYLock.KeyValue = KEY_STARTUP;	
	}
	else if(BLE.flag.b.Is_RX_UnLock)
	{
		BLE.flag.b.Is_RX_UnLock = 0;
		HHYLock.KeyValue = KEY_UNLOCK;	
	}	
}





